from selenium import webdriver
from config.setting import browserpath, time_wite, host_port

#单例模式

class DriverFactory:
    #静态属性
    driver = None

    @classmethod
    def get_driver(cls, browserBox="Chrome"):
        if cls.driver == None:
            if browserBox == "Chrome":
                cls.driver = webdriver.Chrome(browserpath["Chrome"])
                # 浏览器最大化
                cls.driver.maximize_window()
                # 设置隐式等待
                cls.driver.implicitly_wait(time_wite)
                # 调用登录方法
                DriverFactory.login()
            elif browserBox == "Firefox":
                cls.driver = webdriver.Firefox(browserpath["Firefox"])
                # 浏览器最大化
                cls.driver.maximize_window()
                # 设置隐式等待
                cls.driver.implicitly_wait(time_wite)
                # 调用登录方法
                DriverFactory.login()
            elif browserBox == "Edge":
                cls.driver = webdriver.Edge(browserpath["Edge"])
                # 浏览器最大化
                cls.driver.maximize_window()
                # 设置隐式等待
                cls.driver.implicitly_wait(time_wite)
                # 调用登录方法
                DriverFactory.login()
            else:
                print("浏览器类型不支持")

        return cls.driver

    @classmethod
    def login(cls):
        # 打开登录首页
        cls.driver.get(f"http://{host_port}/SharedParkingPlace/login.html")

        # 输入用户名
        cls.driver.find_element_by_id('uname').send_keys("出租方1")
        # 输入密码
        cls.driver.find_element_by_id('upass').send_keys("123")
        # 输入验证码
        code = cls.driver.find_element_by_id('imgcode')
        code.clear()
        code.send_keys("0000")

        # 点击 登录 按钮
        cls.driver.find_element_by_id('button').click()

if __name__ == '__main__':
    DriverFactory.get_driver("Chrome")